﻿/*
 * Created by SharpDevelop.
 * User: user
 * Date: 12/09/2011
 * Time: 13:11
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using csharp.rad.lavanderia.classes;

namespace csharp.rad.lavanderia.views
{
	/// <summary>
	/// Description of frmViewServices.
	/// </summary>
	public partial class frmViewServices : KryptonForm
	{
		V_Services listServices = new V_Services();
		
		public frmViewServices()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void fillDataGridServices()
		{
			int tam= listServices.count();
			String situation="PAGO";
			
			dgvServices.Rows.Clear();
			for(int i=0; i < tam; i++)
			{
				//ABERTO
				if(listServices.get(i).service_situation.Equals("A"))
				{
					situation = "ABERTO";
				}
				else if(listServices.get(i).service_situation.Equals("M")) //PARCIALMENTO PAGO
				{
					situation= "PARCIALMENTE PAGO";
				}
				else if(listServices.get(i).service_situation.Equals("P")) //PAGO
				{
					situation = "PAGO";
				}			
				
				String[] row= new string[]{
					listServices.get(i).service_id,
					listServices.get(i).service_number,
					listServices.get(i).service_date,
					listServices.get(i).customer_name,
					common.Util.formatDouble(listServices.get(i).service_total).ToString("0.00"),
					common.Util.formatDouble(listServices.get(i).service_total_pay).ToString("0.00"),
					common.Util.formatDouble(listServices.get(i).service_total_rest).ToString("0.00"),
					situation
				};
				dgvServices.Rows.Add(row);
			}
		}
		
		void FrmViewServicesLoad(object sender, EventArgs e)
		{
			classes.Customers listCustomers = new csharp.rad.lavanderia.classes.Customers();
			
			listCustomers.fillList();
			
			for(int i=0; i < listCustomers.count(); i++)
			{
				cmbCustomers.Items.Add(listCustomers.get(i));
			}
			cmbCustomers.DisplayMember = "name";
			cmbCustomers.ValueMember = "id";
			
			clear();
			
			search();
		}
		
		void calcFields()
		{
			int tam= listServices.count();
			double total, totalPay, totalRest;
			
			total= totalPay= totalRest= 0;
			for(int i=0; i < tam; i++)
			{
				total += common.Util.formatDouble(listServices.get(i).service_total);
				totalPay+= common.Util.formatDouble(listServices.get(i).service_total_pay);
				totalRest+= common.Util.formatDouble(listServices.get(i).service_total_rest);
			}
			lblNumberRegister.Text= tam.ToString();
			lblTotal.Text= total.ToString("0.00");
			lblTotalPay.Text= totalPay.ToString("0.00");
			lblTotalRest.Text= totalRest.ToString("0.00");
		}
		
		void DgvServicesDoubleClick(object sender, EventArgs e)
		{
			if( dgvServices.SelectedRows.Count <= 0)
			{
				KryptonMessageBox.Show("Selecione um registro da tabela!");
				return;
			}
			String service_id= dgvServices.SelectedRows[0].Cells["service_id"].Value.ToString();
			entries.frmRegisterService form = new csharp.rad.lavanderia.entries.frmRegisterService(service_id);
			form.ShowDialog();
			search();
			//calcFields();
		}
	
		void search()
		{
			string situation   = string.Empty;
			string customer_id = string.Empty;
			//ABERTO
			if(cmbSituation.SelectedIndex.Equals(0))
			{
				situation = "A";
			}
			else if( cmbSituation.SelectedIndex.Equals(1)) //PARCIALMENTO PAGO
			{
				situation= "M";
			}
			else if( cmbSituation.SelectedIndex.Equals(2)) //PAGO
			{
				situation = "P";
			}			
			
			if(cmbCustomers.SelectedIndex >= 0)
				customer_id= (cmbCustomers.SelectedItem as Customer).id;
			
			listServices.fillList(dtpIniteDate.Text, dtpEndDate.Text, customer_id, situation);
			
			fillDataGridServices();
			calcFields();			
		}
		
		void clear()
		{
			dtpIniteDate.Value= Convert.ToDateTime(common.Util.primeiroDiaMes(DateTime.Now.Month, DateTime.Now.Year));
			dtpEndDate.Value= Convert.ToDateTime(common.Util.ultimoDiaMes(DateTime.Now.Month, DateTime.Now.Year));
			
			cmbCustomers.SelectedIndex= -1;
			cmbSituation.SelectedIndex= -1;
		}
		
		void BtnClearFieldsClick(object sender, EventArgs e)
		{
			clear();
		}
		
		void BtnSearchClick(object sender, EventArgs e)
		{
			search();
		}
	}
}
